package com.answer.service.impl;

import com.answer.excepction.BaseException;
import com.answer.mapper.UserMapper;
import com.answer.model.dto.user.UserLoginDTO;
import com.answer.model.dto.user.UserQueryDTO;
import com.answer.model.dto.user.UserRegisterDTO;
import com.answer.model.dto.user.UserUpdateDTO;
import com.answer.model.entity.User;
import com.answer.model.reslut.ResultPage;
import com.answer.model.vo.UserVo;
import com.answer.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author haoshuai
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2024-06-29 15:23:08
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public UserVo login(UserLoginDTO userLoginDTO) {

        User user = baseMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserAccount, userLoginDTO.getUserAccount()));
        if (ObjectUtils.isNotEmpty(user)){
            String pas = DigestUtils.md5DigestAsHex(userLoginDTO.getUserPassword().getBytes());
            String userPassword = user.getUserPassword();
            if (pas.equals(userPassword)){
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(user, userVo);
                return userVo;
            }else {
                throw new BaseException( "密码错误");
            }

        }else {
            throw new BaseException( "用户不存在");
        }
    }

    @Override
    public Integer logout() {
        return null;
    }

    @Override
    public UserVo register(UserRegisterDTO userRegisterDTO) {

        User user = baseMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserAccount, userRegisterDTO.getUserAccount()));

        if (ObjectUtils.isNotEmpty(user)){
            throw new BaseException("用户已存在");
        }
        user = new User();
        BeanUtils.copyProperties(userRegisterDTO, user);
        user.setUserPassword(DigestUtils.md5DigestAsHex(userRegisterDTO.getUserPassword().getBytes()));
        baseMapper.insert(user);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    @Override
    public ResultPage<UserVo> queryAll(UserQueryDTO userQueryDTO) {
        Page page = new Page(userQueryDTO.getCurrent(),userQueryDTO.getSize());
        baseMapper.selectPage(page, getQueryWrapper(userQueryDTO));
        List<User> users = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long total = page.getTotal();
        List<UserVo> userVoList = new ArrayList<>();
        for (User user : users){
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user, userVo);
            userVoList.add(userVo);
        }

        ResultPage resultPage = new ResultPage();
        resultPage.setRecords(userVoList);
        resultPage.setCurrent(current);
        resultPage.setPages(pages);
        resultPage.setTotal(total);
        return resultPage;
    }

    @Override
    public UserVo queryOneById(Long id) {

        User user = baseMapper.selectById(id);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    @Override
    public Boolean update(UserUpdateDTO userUpdateDTO) {
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO, user);
        int i = baseMapper.updateById(user);
        return i == 1;
    }


    private LambdaQueryWrapper<User> getQueryWrapper(UserQueryDTO userQueryDTO) {
        String userName = userQueryDTO.getUserName();
        String userAccount = userQueryDTO.getUserAccount();
        String userRole = userQueryDTO.getUserRole();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userName), User::getUserName, userName);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userAccount), User::getUserAccount, userAccount);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userName), User::getUserRole, userRole);
        return queryWrapper;
    }

}




